package com.gaff.emp.core.modules.perm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.modules.perm.MenuUtil;
import com.gaff.emp.core.modules.perm.controller.menu.form.CreateCatalogForm;
import com.gaff.emp.core.modules.perm.controller.menu.form.CreateMenuForm;
import com.gaff.emp.core.modules.perm.controller.menu.form.UpdateMenuForm;
import com.gaff.emp.core.modules.perm.dao.SysMenuDao;
import com.gaff.emp.core.modules.perm.dao.SysRoleMenuDao;
import com.gaff.emp.core.modules.perm.entity.SysMenu;
import com.gaff.emp.core.modules.perm.entity.SysRoleMenu;
import com.gaff.emp.core.modules.perm.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public List<SysMenu> queryAllMenus() {
        List<SysMenu> menus = sysMenuDao.selectList(Wrappers.emptyWrapper());
        return MenuUtil.mergeMenus(menus);
    }

    @Override
    public SysMenu queryMenuById(Integer id) {
        return sysMenuDao.selectById(id);
    }

    @Override
    public void createCatalog(CreateCatalogForm form) {
        SysMenu menu = sysMenuDao.selectOne(Wrappers.<SysMenu>query().eq("code", form.getCode()));
        if (menu != null) {
            throw new EmpException("菜单代码重复");
        }
        SysMenu catalog = new SysMenu();
        BeanUtil.copyProperties(form, catalog);
        catalog.setParentId(0);
        catalog.setType(SysMenu.catalog);
        sysMenuDao.insert(catalog);
    }

    @Override
    public void createMenu(CreateMenuForm form) {
        SysMenu menu = sysMenuDao.selectOne(Wrappers.<SysMenu>query().eq("code", form.getCode()));
        if (menu != null) {
            throw new EmpException("菜单代码重复");
        }
        if (sysMenuDao.selectById(form.getParentId()) == null) {
            throw new EmpException("上级目录不存在");
        }
        menu = new SysMenu();
        BeanUtil.copyProperties(form, menu);
        menu.setType(SysMenu.menu);
        sysMenuDao.insert(menu);
    }

    @Override
    public void updateMenu(UpdateMenuForm form) {
        SysMenu menu = sysMenuDao.selectById(form.getId());
        if (menu == null) {
            throw new EmpException("菜单不存在");
        }
        BeanUtil.copyProperties(form, menu);
        sysMenuDao.updateById(menu);
    }

    @Override
    public void deleteById(Integer id) {
        SysMenu menu = sysMenuDao.selectById(id);
        if (menu == null) {
            return;
        }
        List<SysMenu> menus = sysMenuDao.selectList(Wrappers.<SysMenu>query().eq("parent_id", id));
        if (menus != null && menus.size() > 0) {
            throw new EmpException("存在子菜单，不能删除");
        }
        List<SysRoleMenu> roleMenus = sysRoleMenuDao.selectList(Wrappers.<SysRoleMenu>query().eq("menu_id", id));
        if (roleMenus != null && roleMenus.size() > 0) {
            throw new EmpException("菜单被角色引用，不能删除");
        }
        sysMenuDao.deleteById(id);
    }

}
